/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Category;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import util.IdbUtil;

/**
 *
 * @author duonghung1369
 */
public class CategoryDAO extends BaseDAO {

    public CategoryDAO(IdbUtil _database) {
        super(_database);
    }

    /**
     * Get all category from table Category
     * @return
     */   
    public List<Category> getCategories() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            List<Category> parent_categories = new ArrayList<Category>();
            String sql = "select * from Category";
            con = _database.getConnection();
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            
            Category temp;
            int parent_id = 0;
            List<Category> child_categories = new ArrayList<Category>();

            while (rs.next()) {
                temp = new Category(rs.getInt("id"), rs.getString("name"));
                parent_id = rs.getInt("parent_id");

                // parent category
                if(parent_id == 0){
                    temp.setParent_category(null);

                    // add parent category to result
                    parent_categories.add(temp);
                }else{ // child category
                    temp.setParent_id(parent_id);

                    // add child category to temp list
                    child_categories.add(temp);
                }
            }

            // add child categories to parent category
            for(Category parent : parent_categories){
                for(Category child : child_categories){
                    if(parent.getId() == child.getParent_id()){
//                        if(parent.getChild_categories() == null){
//                            parent.setChild_categories(new ArrayList<Category>());
//                        }

                        // add child to parent
                        parent.getChild_categories().add(child);

                        // set parent category for child
                        child.setParent_category(parent);
                    }
                }
            }

            return parent_categories;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * Convert categories to a list of list categories that divided to main and sub.
     * @param categories
     * @return
     */
    public List<List<Category>> convertList(List<Category> categories){
        if(categories == null || categories.isEmpty()) {
            return null;
        }

        ArrayList<List<Category>> results = new ArrayList<List<Category>>();
        List<Category> mainList = new ArrayList<Category>();
        List<Category> subList = new ArrayList<Category>();

        for(Category category : categories){
            if(category.getParent_category() == null){
                mainList.add(category);
            }else{
                subList.add(category);
            }
        }

        List<Category> tmpList = new ArrayList<Category>();
        for(Category main : mainList){
            // add main category first
            tmpList.add(main);

            // add sub categories to next
            for(Category sub : subList){
                if(main.getId() == sub.getParent_category().getId()){
                    tmpList.add(sub);
                }
            }

            results.add(tmpList);
            tmpList = new ArrayList<Category>();
        }

        return results;
    }

    @Override
    public Collection getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object get(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object create(Object entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(Object entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    
}
